package my.controller;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import my.interceptor.LoginInterceptor;
import my.model.User;
import my.service.UserService;


/**
 * Created by Rajab on 2017/03/30.
 */
public class MainController extends Controller {

    //用户操作服务
    //@Autowired
    //private static UserService userService;

    public void index() {
        renderFreeMarker("main.ftl");
        //renderHtml("<h1>hello!</h1>");
    }

    public void login() {
        renderFreeMarker("login-new.ftl");
    }

    public void loginCheck() {
        String username = getPara("username", "");
        String password = getPara("password", "");
        UserService.LoginResult loginResult = UserService.checkLogin(username, password);
        boolean success = false;
        switch (loginResult) {
            case LOGIN_OK:
                User user = UserService.findUserByUsername(username);
                if(user != null)
                    setSessionAttr("user", user);
                success = true;
                //redirect("/success");
                setAttr("message","登录成功");
                break;
            case INPUT_INVALID:
                setAttr("message", "用户名或密码为空");
                forwardAction("/login");
                break;
            case PASSWORD_WRONG:
                setAttr("message", "用户名或密码错误");
                forwardAction("/login");
                break;
            case USER_NOT_EXIST:
                setAttr("message","该用户不存在");
                forwardAction("/login");
                break;
        }
        setAttr("success",success);
        renderJson();
    }

    public void reg(){
        renderFreeMarker("reg.ftl");
    }

    public void regCheck(){
        String username = getPara("username", "");
        String password = getPara("password", "");
        String nickname = getPara("nickname", "");
        UserService.RegResult regResult = UserService.checkReg(username, password);
        switch (regResult){
            case INPUT_INVALD:
                setAttr("errMsg","用户名或密码为空");
                forwardAction("/reg");
                break;
            case USER_ALREADY_EXIST:
                setAttr("errMsg","用户已存在");
                forwardAction("/reg");
                break;
            case REG_OK:
                User user = new User();
                user.setUsername(username);
                user.setPassword(password);
                user.setNickname(nickname);
                user.setAdmin(false);
                user.save();
                setAttr("errMsg","注册成功");
                //redirect("/login");
                renderFreeMarker("login-new.ftl");
                break;
        }
    }

    @Before(LoginInterceptor.class)
    public void success() {
        renderFreeMarker("success.ftl");
    }

    public void main() {
        renderFreeMarker("main.ftl");
    }

    public void logout() {
        removeSessionAttr("user");
        renderFreeMarker("main.ftl");
    }

}
